﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Text;

namespace System
{
    public class Dict :BindingList<string>,IDictionary<string,object>
    {
        protected override void InsertItem(int index, string item)
        {
            string[] strs = item.Split(':');
            if (strs.Length == 2)
            {
                ((IDictionary<string, object>)this).Add(strs[0], strs[1]);
            }
            else 
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i < strs.Length; i++)
                    if (sb.ToString() == string.Empty)
                        sb.Append(strs[i]);
                    else
                        sb.Append(string.Format(":{0}",strs[i]));
                ((IDictionary<string, object>)this).Add(strs[0], sb.ToString());
            }

        }

        private Dictionary<string, object> dic = new Dictionary<string, object>();

        void IDictionary<string, object>.Add(string key, object value)
        {
            dic.Add(key, value);
        }

        bool IDictionary<string, object>.ContainsKey(string key)
        {
            return dic.ContainsKey(key);
        }

        ICollection<string> IDictionary<string, object>.Keys
        {
            get
            {
                return dic.Keys;
            }
        }

        bool IDictionary<string, object>.Remove(string key)
        {
            return dic.Remove(key);
        }

        bool IDictionary<string, object>.TryGetValue(string key, out object value)
        {
            return dic.TryGetValue(key,out value);
        }

        ICollection<object> IDictionary<string, object>.Values
        {
            get { return dic.Values; }
        }

        object IDictionary<string, object>.this[string key]
        {
            get
            {
                return dic[key];
            }
            set
            {
                dic[key] = value;
            }
        }

        void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
        {
            ((ICollection<KeyValuePair<string, object>>)dic).Add(item);
        }

        void ICollection<KeyValuePair<string, object>>.Clear()
        {
            ((ICollection<KeyValuePair<string, object>>)dic).Clear();
        }

        bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
        {
            return ((ICollection<KeyValuePair<string, object>>)dic).Contains(item);
        }

        void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            ((ICollection<KeyValuePair<string, object>>)dic).CopyTo(array,arrayIndex);
        }

        int ICollection<KeyValuePair<string, object>>.Count
        {
            get { return ((ICollection<KeyValuePair<string, object>>)dic).Count; }
        }

        bool ICollection<KeyValuePair<string, object>>.IsReadOnly
        {
            get { return ((ICollection<KeyValuePair<string, object>>)dic).IsReadOnly; }
        }

        bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
        {
            return ((ICollection<KeyValuePair<string, object>>)dic).Remove(item);
        }

        IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
        {
            return ((IEnumerable<KeyValuePair<string, object>>)dic).GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((System.Collections.IEnumerable)dic).GetEnumerator();
        }
    }
}